import {Injectable, Inject} from '@angular/core';
import {HttpClient} from '@angular/common/http';
import {Observable} from 'rxjs';
import {map} from 'rxjs/operators';

import {API_CONFIG, ServicesModule} from './services.module';
import {Banner, HotTags, SongSheet} from '../data-types/common.types';

@Injectable({
  providedIn: ServicesModule
})
export class HomeService {

  constructor(
    private http: HttpClient,
    @Inject(API_CONFIG) private uri: string
  ) {
  }

  getBanners(): Observable<Array<Banner>> {
    return this.http.get(`${this.uri}/banner`)
      .pipe(map((res: { banners: Array<Banner> }) => res.banners));
  }

  getHotTags(): Observable<Array<HotTags>> {
    return this.http.get(`${this.uri}/playlist/hot`)
      .pipe(map((res: { tags: Array<HotTags> }) => res.tags
        .sort((a: HotTags, b: HotTags) => a.position - b.position)
          .slice(0, 5)
        )
      );
  }

  getPersonalSheetList(): Observable<Array<SongSheet>> {
    return this.http.get(`${this.uri}/personalized`)
      .pipe(map((res: { result: Array<SongSheet> }) => res.result.slice(0, 16)));
  }

}
